#include "utils.h"
#include<QStringList>
#include<QColor>

Utils::Utils()
{

}

 QByteArray Utils::hexStringtoByteArray(QString hexstr){
    QByteArray qrst;
    QStringList sl;
    hexstr = hexstr.trimmed();
    int n = hexstr.length();
    for(int i = 0;i<n;i+=2){
        sl << hexstr.left(2);
        hexstr = hexstr.mid(2);
    }
    foreach(QString s,sl){
        if(!s.isEmpty())
            qrst.append((char)s.toInt(0,16)&0xFF);
    }
    return qrst;
}

 QStringList Utils::byteArraytoHexString(QByteArray byte){


     QStringList sl;
     int n = byte.length();
     for(int i = 0;i<n;i++){
         int v = byte[i] & 0xFF;
         QString b;
         QString zero = QString('0');
         b = QString::number(v,16);
         if(b.length()<2){
             zero.append(b);
             sl<<zero;
         }else{
             sl<<b;
         }
     }
     return sl;
 }

 int Utils::verify(QByteArray byteArray){
        int n = byteArray.size();
        int count=0;
        for(int i=0;i<n;i++){
            for(int j=0;j<8;j++){
                count +=(byteArray[i]>>j)&0x01;
            }
        }
        return count;
 }

 void Utils::stringToHtmlFilter(QString &str){
     //注意这几行代码的顺序不能乱，否则会造成多次替换
        str.replace("&","&amp;");
        str.replace(">","&gt;");
        str.replace("<","&lt;");
        str.replace("\"","&quot;");
        str.replace("\'","&#39;");
        str.replace(" ","&nbsp;");
        str.replace("\n","<br>");
        str.replace("\r","<br>");
 }

 void Utils::stringToHtml(QString &str, QColor crl){
     QByteArray array;
          array.append(crl.red());
          array.append(crl.green());
          array.append(crl.blue());
          QString strC(array.toHex());
          str = QString("<span style=\" color:#%1;\">%2</span>").arg(strC).arg(str);
 }



